package com.sixseven.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixseven.bean.AnnAndComs;
import com.sixseven.bean.Announcement;
import com.sixseven.bean.Comment;
import com.sixseven.service.AnnouncementService;
import com.sixseven.service.CommentService;
import com.sixseven.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Belly
 * @version 1.1.0
 */
@RestController
@Slf4j
public class AnnAndComsController {
    @Resource
    private AnnouncementService announcementService;
    @Resource
    private CommentService commentService;
    @Value("${pictureFile.path}")
    private String absolutePath;
    @PostMapping("/annList")
    public Result announcementList(){
        Map<Integer, AnnAndComs> annAndComsMap = announcementService.AnnouncementAndComments();
        if (annAndComsMap == null){
            return Result.error();
        }
        return Result.success(annAndComsMap);
    }
    @GetMapping("/addComment/{announcementId}")
    public Result addComment(@RequestParam String message, @PathVariable Integer announcementId, HttpServletRequest request){
        if (!StringUtils.hasText(message)){
            return Result.error();
        }
        if (announcementId == null){
            return Result.error();
        }
        Announcement announcement = announcementService.getById(announcementId);
        if (announcement == null){
            return Result.error();
        }
        int i = commentService.addComment(message, announcementId, request);
        return i == 1 ? Result.success("200", "评论成功") : Result.error();
    }
    @RequestMapping("/adminAnnList")
    public Result adminAnnList(@RequestParam(defaultValue = "") String searchName,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "3") Integer pageSize){
        LambdaQueryWrapper<Announcement> announcementLambdaQueryWrapper = Wrappers.lambdaQuery();
        announcementLambdaQueryWrapper.like(Announcement::getText, searchName);
//        List<Announcement> announcementList =
//                announcementService.list(announcementLambdaQueryWrapper);
        Page<Announcement> announcementPage =
                announcementService.page(
                        new Page<>(pageNum, pageSize), announcementLambdaQueryWrapper);
        if (announcementPage == null){
            return Result.error();
        }
        return Result.success(announcementPage);
    }
    @RequestMapping("/addAnnouncement")
    public Result addAnnouncement(@RequestBody Announcement announcement){
        if (announcement == null){
            return Result.error();
        }
        announcement.setDatetime(new Date());
        boolean save = announcementService.save(announcement);
        if (!save){
            return Result.error();
        }
        return Result.success("200", "添加成功");
    }
    @DeleteMapping("/delAnnouncement/{id}")
    public Result delAnnouncement(@PathVariable Integer id){
        if (id == null){
            return Result.error();
        }
        //得到图片的路径
        String imgPath = announcementService.getById(id).getImgPath();
        if (!imgPath.isEmpty()){
            File filePathDirectory = new File(absolutePath + imgPath);
            if (filePathDirectory.exists()){
                filePathDirectory.delete();//删除旧照片
            }
        }
        boolean removeById = announcementService.removeById(id);
        if (!removeById){
            return Result.error();
        }
        return Result.success("200", "删除成功");
    }
    @PutMapping("/updateAnnouncement")
    public Result updateAnnouncement(@RequestBody Announcement announcement){
        if (announcement == null){
            return Result.error();
        }
        boolean updateById = announcementService.updateById(announcement);
        if (!updateById){
            return Result.error();
        }
        return Result.success("200", "修改成功");
    }
    @GetMapping("/commentList")
    public Result commentList(@RequestParam Integer announcementId){
        if (announcementId == null){
            return Result.error();
        }
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = Wrappers.lambdaQuery();
        commentLambdaQueryWrapper.eq(Comment::getAnnouncementId, announcementId);
        List<Comment> commentList = commentService.list(commentLambdaQueryWrapper);
        return Result.success(commentList);
    }
}
